package com.yipao.db.service.goods;

import com.github.pagehelper.PageHelper;
import com.yipao.db.constant.DbConstant;
import com.yipao.db.dao.goods.GoodsFormulaAttrMapper;
import com.yipao.db.dao.goods.GoodsFormulaMapper;
import com.yipao.db.dao.goods.GoodsMaterialMapper;
import com.yipao.db.domain.goods.GoodsFormula;
import com.yipao.db.domain.goods.GoodsFormulaAttr;
import com.yipao.db.domain.goods.GoodsMaterial;
import com.yipao.db.domain.goods.vo.formula.GoodsFormulaDetailResp;
import com.yipao.db.domain.goods.vo.formula.GoodsFormulaReq;
import com.yipao.db.domain.goods.vo.formula.GoodsFormulaResp;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品配方
 *
 * @author tianhao
 * @date 2020/4/29 19:03
 **/
@Service
public class GoodsFormulaService {
    @Resource
    private GoodsFormulaMapper mapper;
    @Resource
    private GoodsFormulaAttrMapper goodsFormulaAttrMapper;
    @Resource
    private GoodsMaterialMapper goodsMaterialMapper;


    public GoodsFormula findOne(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }


    /**
     * 新增
     *
     * @param record
     * @return
     */
    public int insert(@RequestBody GoodsFormula record) {
        record.setAddTime(LocalDateTime.now());
        record.setUpdateTime(record.getAddTime());
        record.setDeleted(DeleteStatusConstant.NOT_DELETED);
        return mapper.insert(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    public int update(@RequestBody GoodsFormula record) {
        record.setUpdateTime(LocalDateTime.now());
        return mapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件查询商品配方
     *
     * @param goodsName 产品名称
     * @param page      第几页
     * @param limit     每页显示的记录数
     * @param sort      排序字段,默认创建时间
     * @param order     排序规则,默认降序,即先创建的,排在前面
     * @param
     * @return
     */
    public List<GoodsFormulaResp> adminList(String goodsName,
                                            Integer page,
                                            Integer limit,
                                            String sort,
                                            String order) {
        GoodsFormulaReq goodsFormulaReq = new GoodsFormulaReq(goodsName, page, limit, sort, order,
                DeleteStatusConstant.NOT_DELETED);
        PageHelper.startPage(page, limit);
        List<GoodsFormulaResp> list = mapper.selectByExample(goodsFormulaReq);
        if (!list.isEmpty()) {
            String materialId;
            // TODO: 2020/5/25 此接口待优化,查询过多
            for (GoodsFormulaResp goodsFormulaResp : list) {
                materialId = goodsFormulaResp.getMaterialId();
                // 包含多个原料
                if (materialId.contains(DbConstant.COMMA)) {
                    String[] materialIdArray = materialId.split(DbConstant.COMMA);
                    for (String id : materialIdArray) {
                        // 根据原料id查询原料名称
                        goodsFormulaResp.setName(goodsFormulaResp.getName() + DbConstant.DIAGONAL + goodsMaterialMapper.queryNameById(id));
                    }
                }
                // 温度名称
                if (!StringUtils.isEmpty(goodsFormulaResp.getTempAttrId())) {
                    goodsFormulaResp.setTempAttrName(String.join(DbConstant.DIAGONAL, goodsFormulaAttrMapper.queryNameById(goodsFormulaResp.getTempAttrId())));
                }
                // 浓度
                if (!StringUtils.isEmpty(goodsFormulaResp.getDenseAttrId())) {
                    goodsFormulaResp.setDenseName(String.join(DbConstant.DIAGONAL, goodsFormulaAttrMapper.queryNameById(goodsFormulaResp.getDenseAttrId())));
                }
                // 浓度
                if (!StringUtils.isEmpty(goodsFormulaResp.getSweetAttrId())) {
                    goodsFormulaResp.setSweetName(String.join(DbConstant.DIAGONAL, goodsFormulaAttrMapper.queryNameById(goodsFormulaResp.getSweetAttrId())));
                }
            }
        }
        return list;
    }


    /**
     * 逻辑删除
     *
     * @param id 主键
     * @return
     */
    public void delete(Integer id) {
        GoodsFormula goodsFormula = new GoodsFormula();
        goodsFormula.setDeleted(DeleteStatusConstant.IS_DELETED);
        goodsFormula.setId(id);
        goodsFormula.setUpdateTime(LocalDateTime.now());
        mapper.updateDeleteStatus(goodsFormula);
    }

    /**
     * 查询配方下的所有属性
     *
     * @param goodsFormulaId 配方id
     * @return
     */
    public GoodsFormulaDetailResp queryGoodsFormulaByGoodsId(Integer goodsFormulaId) {
        GoodsFormulaDetailResp goodsFormulaDetailResp = new GoodsFormulaDetailResp();
        GoodsFormula goodsFormula = mapper.selectByPrimaryKey(goodsFormulaId);
        if (goodsFormula == null || StringUtils.isEmpty(goodsFormula.getId())) {
            return goodsFormulaDetailResp;
        }
        // 原料id
        String materialId = goodsFormula.getMaterialId();
        if (!StringUtils.isEmpty(materialId)) {
            goodsFormulaDetailResp.setMaterialId(goodsFormula.getMaterialId());
            // 根据原料Id,查询原料名称
            goodsFormulaDetailResp.setMaterialName(goodsMaterialMapper.queryNameById(materialId));
        }
        // 配方名称
        goodsFormulaDetailResp.setName(goodsFormula.getName());
        goodsFormulaDetailResp.setId(goodsFormulaId);


        // 查询配方下的所有属性id
        List<String> attrIds = this.queryFormulaAttrIdById(goodsFormulaId);
        if (attrIds.isEmpty()) {
            return goodsFormulaDetailResp;
        }
        // 根据配方属性Id查询配方属性值
        List<GoodsFormulaAttr> goodsFormulas = goodsFormulaAttrMapper.queryGoodsFormulaByAttrIds(attrIds, DeleteStatusConstant.NOT_DELETED);
        if (goodsFormulas.isEmpty()) {
            return goodsFormulaDetailResp;
        }

        // 根据code分组
        goodsFormulaDetailResp.setAttr(goodsFormulas.stream().collect(Collectors.groupingBy(GoodsFormulaAttr::getCode)));

        return goodsFormulaDetailResp;
    }

    /**
     * 查询所有配方(id,name),用做下拉框
     *
     * @return
     */
    public List<GoodsFormula> queryAllFormula() {
        return mapper.queryAllFormula(DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 查询配方下的所有属性id
     *
     * @param goodsFormulaId 配方属性id
     * @return
     */
    public List<String> queryFormulaAttrIdById(Integer goodsFormulaId) {
        GoodsFormula goodsFormula = mapper.queryFormulaAttrIdById(goodsFormulaId);
        if (goodsFormula == null) {
            return new ArrayList<>();
        }
        List<String> attrIds = new ArrayList<>();
        String tempAttrId = goodsFormula.getTempAttrId();
        if (!StringUtils.isEmpty(tempAttrId)) {
            attrIds.addAll(Arrays.asList(tempAttrId.split(DbConstant.COMMA)));
        }
        String denseAttrId = goodsFormula.getDenseAttrId();
        if (!StringUtils.isEmpty(denseAttrId)) {
            attrIds.addAll(Arrays.asList(denseAttrId.split(DbConstant.COMMA)));
        }
        String sweetAttrId = goodsFormula.getSweetAttrId();
        if (!StringUtils.isEmpty(sweetAttrId)) {
            attrIds.addAll(Arrays.asList(sweetAttrId.split(DbConstant.COMMA)));
        }
        return attrIds;
    }

    /**
     * 根据原料id查询商品配方数量
     *
     * @param materialId 原料id
     * @return
     */
    public int queryCountFormulaByMaterialId(Integer materialId) {
        return mapper.queryCountFormulaByMaterialId(materialId, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 根据配方属性id,查询数量
     *
     * @param attrId 配方属性id
     * @return
     */
    public int queryCountFormulaAttrByAttrId(Integer attrId, String code) {
        return mapper.queryCountFormulaAttrByAttrId(attrId, code, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 根据原料id查询配方参数
     *
     * @param formulaId
     * @return
     */
    public String queryMaterialDevParamByFormulaId(Integer formulaId) {
        return mapper.queryMaterialDevParamByFormulaId(formulaId);
    }

}
